<include file="Public/header" />
<title></title>
</head>
<style type="text/css">
    .table span{
        display: block;
        float: left;
        margin-right: 12px;;
    }
</style>
<body>
<div class="page-container">
    <form class="form form-horizontal" id="form-article-add">
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>角色名称：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="" placeholder=""  name="name">
            </div>
        </div>
        <div class="row cl">
            <table class="table">
                <tr><td width="100">模块</td><td>权限</td></tr>
            </table>
        </div>
        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
                <button onClick="save()" class="btn btn-secondary radius" type="button"><i class="Hui-iconfont">&#xe632;</i> 保存</button>
                <button onClick="cancel()" class="btn btn-default radius" type="button">&nbsp;&nbsp;取消&nbsp;&nbsp;</button>
            </div>
        </div>
    </form>
</div>
<include file="Public/footer_js" />
<script>
    function save(){
        var group_id='';
        for(var i=0;i<$(".table input:checked").length;i++){
            group_id+=','+$(".table input:checked").eq(i).val();
        }
        if($("input[name=name]").val()==''){
            layer.msg('角色名称不能为空');
            return false;
        }
        if($("input[name=name]").val().length<2 || $("input[name=name]").val().length>24){
            layer.msg('角色名称长度范围为2-24');
            return false;
        }
        if(group_id==''){
            layer.msg('请添加权限');
            return false;
        }
        $.post('__CONTROLLER__/department_addD',{
            name:$("input[name=name]").val(),
            group_id:group_id.substring(1),
        },function (msg){
            layer.msg(msg.data);
            if(msg.flag==1){
                setTimeout(function (){
                    parent.location.reload();
                    parent.layer.closeAll();
                },800);
            }
        },'json');
    }
    function cancel(){
        parent.layer.closeAll();
    }
    ////////获取权限
    $.get('__MODULE__/Role/getRole',{},function (msg){
        getRole(msg.data);
    },'json');
    function getRole(data){
        for(var m in data){
            $(".table").append($('<tr><td><label><input value="'+data[m]['id']+'" type="checkbox" />'+data[m]['name']+'</label></td></tr>'));
            if(data[m]['name']){
                var n_data=data[m]['child'];
                var h='';
                for(var k in n_data){
                    h+='<span><label><input value="'+n_data[k]['id']+'" type="checkbox" />'+n_data[k]['name']+'</label></span>';
                }
                $(".table").find('tr').eq($(".table").find('tr').length-1).append($("<td>"+h+"</td>"));
            }
        }
    }
    $(".table").on('click','label',function (){
        if($(this).parents('tr').find('input').prop('value')==$(this).children().prop('value')){
            /////如果选中的是 模块  那么相关的多选框都要被选中
            $(this).parent().next().find('input').prop("checked",$(this).children().prop('checked'));
        }
    });
</script>
</body>
</html>